$(function () {
    let layer = layui.layer;
    let form = layui.form;
    let laypage = layui.laypage;



    let q = {
        "pagenum": 1,
        "pagesize": 5,

    }
    // 时间规范过滤器
    template.defaults.imports.dateFormat = function (date) {
        let newDate = new Date(date)

        let y = newDate.getFullYear()
        let m = padZero(newDate.getMonth() + 1)
        let d = padZero(newDate.getDate())
        let hh = padZero(newDate.getHours())
        let mm = padZero(newDate.getMinutes())
        let ss = padZero(newDate.getSeconds())

        return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
    }

    // 补零
    function padZero(str) {
        return str > 9 ? str : '0' + str
    }

    // 获取文章列表
    function getList() {
        $.ajax({
            method: 'get',
            url: '/my/article/list',
            data: q,
            success: function (res) {
                console.log(res);
                if (res.status !== 0) {
                    return layer.msg('获取文章列表失败')
                }
                let txt = template('tpl-list', res);
                $("tbody").html(txt)
                renderPage(res.total)
            }
        })
    }

    // 获取文章类别
    function getCategory() {
        $.ajax({
            method: 'get',
            url: '/my/article/cates',
            success: function (res) {

                if (res.status !== 0) {
                    return layer.msg('类别获取失败')
                }
                let txt = template('tpl-cate', res);

                $("[name=cate_id]").html(txt)

                form.render()

            }
        })
    }
    // 调用获取类别函数
    getCategory();

    // 调用获取文章列表函数
    getList();

    // 筛选表单
    $('#searchForm').on('submit', function (e) {

        e.preventDefault()

        q.cate_id = $('[name=cate_id]').val()
        q.state = $('[name=state]').val()

        if (!q.cate_id) {
            q.cate_id = undefined;
        }

        if (!q.state) {
            q.state = undefined;
        }
        getList()

    })

    // 渲染分页区域
    function renderPage(total) {
        laypage.render({
            elem: 'pageBox',
            count: total,
            limit: q.pagesize,
            curr: q.pagenum,
            limits: [2, 3, 5, 10],
            layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
            jump: function (obj, first) {
                q.pagesize = obj.limit
                q.pagenum = obj.curr
                if (!first) {
                    getList()
                }
            }

        });
    }

    // 点击删除按钮
    $('tbody').on('click', '.deleteBtn', function () {

        let id = $(this).attr('data-id')
        layer.confirm('是否删除？', { icon: 3, title: '提示' }, function (index) {


            let len = $('.deleteBtn').length
            console.log(id);
            console.log(len);
            $.ajax({
                method: 'GET',
                url: '/my/article/delete/' + id,
                success: function (res) {
                    console.log(res);
                    if (res.status !== 0) {
                        return layer.msg('删除失败')
                    }
                    layer.msg('删除成功')

                    if (len === 1) {
                        console.log('没了');
                        q.pagenum = q.pagenum === 1 ? 1 : q.pagenum - 1
                        console.log(q.pagenum);
                    }

                    getList()
                }
            })

            layer.close(index);
        });
    })


    // 点击编辑按钮
    $('tbody').on('click', '.editBtn', function () {
        var id = $(this).attr('data-id')
        console.log(id);
        location.href = `./publish.html?id=${id}`
    })



})